from pub.routine import TcpClient
# from pub.routine import UdpClient
from broker.routine import UdpImgClient as UdpClient
from broker.protocal import ReplyForwardTrans, BROKER_PORT

BROKER_ADDR = "127.0.0.1"

if __name__ == "__main__":
    client = UdpClient((BROKER_ADDR, BROKER_PORT))
    protocal = ReplyForwardTrans()
    client.set_protocal(protocal)
    client.listen(runHeart=True)

    # 查询当前协助列表
    protocal.register_list()
    ip_addr = input("输入连接的License序列号: ")
    protocal.connect_to_remote(ip_addr)

    # 由于无法区分是对端是否还存在（主动还是被动关闭），使用try结构
    try:
        input("Input Anything to quit: ")
        protocal.connect_close()  # 主动关闭
    except OSError:  # 被动关闭时，对端socket已close，导致OSError异常
        # OSError: [WinError 10038] 在一个非套接字上尝试了一个操作
        pass

    client.stop()
    print("End")
